package com.guoyw.leetcode.question.everyday.Y2021.M03;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
 * @Description: 61. 旋转链表
 * @Author: guoyw
 * @CreateDate: 2021/3/26 2:02 下午
 * @Version: 1.0
 */
@Slf4j
public class Day27_C61 {
 // 看完解析 其实用的还是闭环放，开始直接连层环，在适当地方断开、
  // 自己写的思想是一样的 但代码写邋遢了
  public static void main(String[] args) {
    ListNode listNode1 = new ListNode(2,null);
    ListNode listNode2 = new ListNode(1,listNode1);
    ListNode listNode3 = new ListNode(0,listNode2);

    ListNode listNode = deleteDuplicates(listNode3, 1);
    log.info(listNode.toString());

  }

  public static ListNode deleteDuplicates(ListNode head, int k) {
    if(head == null || head.next == null){
      return head;
    }

    ListNode cursor = head;
    int num = 1;
    while (cursor.next != null) {
       num++;
      cursor = cursor.next;
    }

    int mk = num - k%num;
    if (mk==num) {
      return head;
    }

    ListNode b = head;
    while (mk-- > 0) {
      b = b.next;
    }
    ListNode c = b;
    while (b.next != null) {
        b = b.next;
    }
    b.next = head;

    ListNode d = c;
    while (num-- > 1) {
      c = c.next;
    }
    c.next = null;
    return d;
  }


  public static class ListNode {
    int val;
    ListNode next;

    ListNode() {
    }

    ListNode(int val) {
      this.val = val;
    }

    ListNode(int val, ListNode next) {
      this.val = val;
      this.next = next;
    }
  }
}
